﻿/*
 * BFind - A tool for finding football matches for trading on Betfair
 * Copyright 2009, 2010 Magnus Olsson <magnus72(at)gmail.com>
 * 
 * This file is part of BFind.
 * 
 * BFind is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * BFind is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with BFind.  If not, see <http://www.gnu.org/licenses/>
*/

using System;
using System.Collections.Generic;

namespace betfairfind.Rules
{
    class O25HT00FT00 : BaseRule
    {


        double MIN_HT00_ODDS = 10;
        double MAX_HT00_ODDS = 15;
        double MIN_FT00_ODDS = 10;
        double MAX_FT00_ODDS = 15;
        double MIN_CS_MATCHED = 500;

        double MIN_OVER25_ODDS = 1;
        double MAX_OVER25_ODDS = 2;

        public O25HT00FT00(Dictionary<string, string> config, List<MyMarket> inplaymarkets, Dictionary<long, MarketPricesCompressed> marketprices)
            : base()
        {

            MarketsUsedInside = new string[] { "half time score", "over/under 2.5 goals", "correct score" };


            try
            {

                MIN_HT00_ODDS = Convert.ToDouble(config["Rule.O25HT00FT00.MIN_HT00_ODDS"]);
                MAX_HT00_ODDS = Convert.ToDouble(config["Rule.O25HT00FT00.MAX_HT00_ODDS"]);
                MIN_FT00_ODDS = Convert.ToDouble(config["Rule.O25HT00FT00.MIN_FT00_ODDS"]);
                MAX_FT00_ODDS = Convert.ToDouble(config["Rule.O25HT00FT00.MAX_FT00_ODDS"]);
                MIN_CS_MATCHED = Convert.ToDouble(config["Rule.O25HT00FT00.MIN_CS_MATCHED"]);

                MIN_OVER25_ODDS = Convert.ToDouble(config["Rule.O25HT00FT00.MIN_OVER25_ODDS"]);
                MAX_OVER25_ODDS = Convert.ToDouble(config["Rule.O25HT00FT00.MAX_OVER25_ODDS"]);

            }
            catch (Exception e)
            {
                Logger.LogError(e.ToString(), true, true);
            }




            string criteria = @"" + MIN_HT00_ODDS + "/" +
                                    MAX_HT00_ODDS + "/" +
                                    MIN_FT00_ODDS + "/" +
                                    MAX_FT00_ODDS + "/" +
                                    MIN_OVER25_ODDS + "/" +
                                    MAX_OVER25_ODDS + "/" +
                                    MIN_CS_MATCHED;

            Logger.Log("============================================================================", true, true);
            Logger.Log("Matches qualified for Correct score 0-0, HT 0-0, Over 2.5 (" + criteria + ")", true, true);
            Logger.Log("----------------------------------------------------------------------------", true, true);

            if (!CheckRequiredMarkets()) { return; }


            string[] games = FindSoccerGames.GetMatches();

            int selectionCount = 0;
            foreach (var game in games)
            {
                try
                {
                    MyMarket market_ou = FindSoccerGames.GetMarket(game, "Over/Under 2.5 goals");
                    MarketPricesCompressed mpc_ou = marketprices[market_ou.marketId];

                    MyMarket market_cs = FindSoccerGames.GetMarket(game, "correct score");
                    MarketPricesCompressed mpc_cs = marketprices[market_cs.marketId];

                    MyMarket market_ht = FindSoccerGames.GetMarket(game, "half time score");
                    MarketPricesCompressed mpc_ht = marketprices[market_ht.marketId];



                    double matched_cs = mpc_cs.TotalMatched;
                    double ht00odds = mpc_ht.GetBestLayOdds(0);
                    double ft00odds = mpc_cs.GetBestBackOdds(0);
                    double o25odds = mpc_ou.GetBestBackOdds(1);


                    if (matched_cs >= MIN_CS_MATCHED &&
                        ht00odds >= MIN_HT00_ODDS &&
                        ht00odds <= MAX_HT00_ODDS &&
                        ft00odds >= MIN_FT00_ODDS &&
                        ft00odds <= MAX_FT00_ODDS &&
                        o25odds >= MIN_OVER25_ODDS &&
                        o25odds <= MAX_OVER25_ODDS)
                    {
                        Logger.Log(market_cs.marketTime + ", " + BF.convertPathToGame(game) + ", HT 0-0: " + ht00odds + ", FT 0-0: " + ft00odds + ", over 2.5: " + o25odds + ", " + config["CURRENCY_SIGN"] + matched_cs, true, true);
                        selectionCount++;
                    }

                }
                catch (Exception ex)
                {

                    Logger.LogError("Error in game " + game + Environment.NewLine + ex.ToString(), true, false);

                }

            }
            if (selectionCount == 0)
            {
                Logger.Log("No matches", true, true);
            }
        }
    }
}
